﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CIT.WMS.output
{
    public partial class FrmMetaOut : Form
    {
        public FrmMetaOut()
        {
            InitializeComponent();
        }

        private void txt_reelid_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                uploadData();
            }
        }

        private void btn_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_yes_Click(object sender, EventArgs e)
        {
            uploadData();
        }
        BarcodeUtils obj = new BarcodeUtils();

        void uploadData()
        {
            if (txt_reelid.Text.Length == 0)
            {
                lab_msg.Text = "请输入料卷号.";
                lab_msg.ForeColor = Color.Red;
                txt_reelid.SelectAll();
                txt_reelid.Focus();
                return;
            }
            BarcodeUtils.BarObject barobj = obj.AnalysisBarcode(Utils.AnalysisSerPath, txt_reelid.Text);

            Utils.uContext.Accountk__BackingField = "mes40";
            DataTable dt = Utils.NMS.QueryDataTable(Utils.uContext, "select * from mdcdatvstorage where reelid='" + barobj.reelId + "'", new CIT.WMS.MESWcf.CmdParameter[0]);
            if (dt.Rows.Count == 0)
            {
                lab_msg.Text = "料卷不存在.";
                lab_msg.ForeColor = Color.Red;
                txt_reelid.SelectAll();
                txt_reelid.Focus();
                return;
            }
            lab_msg.Text = "";
            if (dt.Rows[0]["isenable"].ToString() == "1")
            {
                lab_msg.Text = "料卷被禁用.";
                lab_msg.ForeColor = Color.Red;
                txt_reelid.SelectAll();
                txt_reelid.Focus();
                return;
            }
            else if (dt.Rows[0]["housecode"].ToString() == "007")
            {
                lab_msg.Text = "料卷尚未入库.";
                lab_msg.ForeColor = Color.Red;
                txt_reelid.SelectAll();
                txt_reelid.Focus();
                return;
            }
            else if (dt.Rows[0]["housecode"].ToString() == "017")
            {
                lab_msg.Text = "料卷已经退库.";
                lab_msg.ForeColor = Color.Red;
                txt_reelid.SelectAll();
                txt_reelid.Focus();
                return;
            }
            else if (dt.Rows[0]["housecode"].ToString() == "011")
            {
                lab_msg.Text = "料卷在分析中心.";
                lab_msg.ForeColor = Color.Red;
                txt_reelid.SelectAll();
                txt_reelid.Focus();
                return;
            }
            else if (dt.Rows[0]["housecode"].ToString() == "099")
            {
                lab_msg.Text = "标签已报废.";
                lab_msg.ForeColor = Color.Red;
                txt_reelid.SelectAll();
                txt_reelid.Focus();
                return;
            }
            if (chx_auto.Checked)
            {
                AppropriationSheet appSheet = new AppropriationSheet();
                appSheet.WOCode = dt.Rows[0]["wocode"].ToString();
                appSheet.User = Utils.uContext.UserNamek__BackingField;
                appSheet.Slot = dt.Rows[0]["slot"].ToString();
                appSheet.Side = dt.Rows[0]["side"].ToString();
                appSheet.ReelID = barobj.reelId;
                appSheet.Qty = dt.Rows[0]["qty"].ToString();
                appSheet.POCode = dt.Rows[0]["pocode"].ToString();
                appSheet.PartNumber = dt.Rows[0]["partnumber"].ToString();
                appSheet.MType = dt.Rows[0]["mtype"].ToString();
                appSheet.MachineID = dt.Rows[0]["machid"].ToString();
                appSheet.Line = dt.Rows[0]["line"].ToString();
                appSheet.LaneNo = dt.Rows[0]["laneno"].ToString();
                appSheet.HouseCode = "017";
                appSheet.FromHouse = dt.Rows[0]["housecode"].ToString();
                appSheet.Description = "PDA料卷退供应商";
                Utils.SaveAppropriationSheet(appSheet);
                decimal qty = 0;
                try
                {
                    qty = Convert.ToDecimal(dt.Rows[0]["qty"].ToString());
                }
                catch { }
                string sql = "update mdcdatvstorage set housecode='017' where reelid='" + barobj.reelId + "' update mdcdatrstorage set qty=qty-" + qty + " where partnumber='" + dt.Rows[0]["partnumber"].ToString() + "'";
                //增加SyssystemLog日志 nancy 2017.07.26
                sql += " insert SyssystemLog(Fguid,line,WoCode,SfcNo,ProuctCode,PartNumber,ReelID,pocode,SupplierID,HouseCode,Qty,Remarks,Creator) ";
                sql += " select newid(),line,wocode,sfcno,'',PartNumber,ReelID,pocode,SupplierID,'017',Qty,'PDA料卷退供应商', '" + Utils.uContext.UserNamek__BackingField + "' from mdcdatvstorage where reelid='" + txt_reelid.Text + "'";  
                bool flag = false;
                bool result = false;
                Utils.NMS.Exectransql(Utils.uContext, sql, new CIT.WMS.MESWcf.CmdParameter[0], out flag, out result);
                lab_msg.Text = "退库成功.";
                lab_msg.ForeColor = Color.Lime;
                txt_reelid.Text = "";
                txt_reelid.Focus();
            }
            else
            {
                if (MessageBox.Show("是否确认退料.", "开铭科技温馨提醒", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                {
                    AppropriationSheet appSheet = new AppropriationSheet();
                    appSheet.WOCode = dt.Rows[0]["wocode"].ToString();
                    appSheet.User = Utils.uContext.UserNamek__BackingField;
                    appSheet.Slot = dt.Rows[0]["slot"].ToString();
                    appSheet.Side = dt.Rows[0]["side"].ToString();
                    appSheet.ReelID = barobj.reelId;
                    appSheet.Qty = dt.Rows[0]["qty"].ToString();
                    appSheet.POCode = dt.Rows[0]["pocode"].ToString();
                    appSheet.PartNumber = dt.Rows[0]["partnumber"].ToString();
                    appSheet.MType = "3";
                    appSheet.MachineID = dt.Rows[0]["machid"].ToString();
                    appSheet.Line = dt.Rows[0]["line"].ToString();
                    appSheet.LaneNo = dt.Rows[0]["laneno"].ToString();
                    appSheet.HouseCode = "017";
                    appSheet.FromHouse = dt.Rows[0]["housecode"].ToString();
                    appSheet.Description = "PDA料卷退供应商";
                    Utils.SaveAppropriationSheet(appSheet);
                    decimal qty = 0;
                    try
                    {
                        qty = Convert.ToDecimal(dt.Rows[0]["qty"].ToString());
                    }
                    catch { }
                    string sql = "update mdcdatvstorage set housecode='017' where reelid='" + barobj.reelId + "' update mdcdatrstorage set qty=qty-" + qty + " where partnumber='" + dt.Rows[0]["partnumber"].ToString() + "'";
                    sql += " insert SyssystemLog(Fguid,line,WoCode,SfcNo,ProuctCode,PartNumber,ReelID,pocode,SupplierID,HouseCode,Qty,Remarks,Creator) ";
                    sql += " select newid(),line,wocode,sfcno,'',PartNumber,ReelID,pocode,SupplierID,'017',Qty,'PDA料卷退供应商', '" + Utils.uContext.UserNamek__BackingField + "' from mdcdatvstorage where reelid='" + txt_reelid.Text + "'";  
                    bool flag = false;
                    bool result = false;
                    Utils.NMS.Exectransql(Utils.uContext, sql, new CIT.WMS.MESWcf.CmdParameter[0], out flag, out result);
                    lab_msg.Text = "退库成功.";
                    lab_msg.ForeColor = Color.Lime;
                    txt_reelid.Text = "";
                    txt_reelid.Focus();
                }
            }
        }
    }
}